<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\model\Article;
use app\common\model\ArticleCate;
use app\common\model\Content;
use app\common\model\Images;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class AdminImages extends AdminInit
{
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        $list = Content::with(["images"])
            ->order("id DESC")
            ->where("model_id",4)
            ->paginate(["query"=>request()->param()]);
        //p($list->toArray());
        View::assign("list",$list);
        return  view();
    }


    /**
     *      *
     * @return \think\Response
     */
    public function create()
    {
        if(Request::isAjax()){
            $user = $this->user;
            $d = input("post.");
            $d["model_id"] = 4;
            $d["uid"] = $user["id"];
            $d["userinfo"] = json_encode($user);
            $content = new Content();
            $res = $content->save($d);
            $contentid = $content->id;
            $d["contentid"] = $contentid;
            $images = Images::create($d);
            if($res){
                return  json(["code"=>200,"msg"=>"成功"]);
            }else{
                return  json(["code"=>4000,"msg"=>"请稍后再试"]);
            }

        }
        View::assign("res",[]);
        View::assign("title","文章发布");
        return \view();
    }



    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $res = Content::with(["images"])->find($id);
        View::assign("res",$res);
        return view('create');
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    public function piliang()
    {
        if(Request::isAjax()){
            $d = input("param.");
            $user = $this->user;
            foreach ($d["img"] as $k => $v){
                $temp["model_id"] = 4;
                $temp["uid"] = $user["id"];
                $temp["title"] = $d["name"][$k];
                $temp["info"] = $v;
                $temp["img"] = $v;
                $temp["status"] = $d["status"];
                $temp["userinfo"] = json_encode($user);
                //p($temp);
                $content = new Content();
                $res = $content->save($temp);
                $contentid = $content->id;
                $temp["contentid"] = $contentid;
                $images = Images::create($temp);

            }
            if($res){
                return  json(["code"=>200,"msg"=>"成功"]);
            }else{
                return  json(["code"=>4000,"msg"=>"请稍后再试"]);
            }

        }
        $res = [];
        View::assign("res",$res);
        return view();
    }

    public function change_status($id)
    {
        $res = Content::with(["images"])->find($id);
        p($res);die;
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $res = Content::with(["images"])->find($id);
        p($res);die;
        $res = ArticleCate::where("id",$id)->delete();
        if($res){
            return  json(["code"=>200,"msg"=>"成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }

    }

}
